package com.xxljob.demo.controller;

import com.xxl.job.core.biz.model.ReturnT;
import com.xxljob.demo.core.server.EmbedServer;
import com.xxljob.demo.core.thread.JobTriggerPoolHelper;
import com.xxljob.demo.core.trigger.TriggerTypeEnum;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Author：YH
 * @Description：
 * @Date:Created in 2022/5/23 11:36
 */
@Controller
@RequestMapping("/jobInfo")
public class JobInfoController {


    @Resource
    private EmbedServer embedServer;

    @RequestMapping("/embedServer")
    @ResponseBody
    public ReturnT<String> triggerEmbedServer(String address, int port, String appname, String accessToken) {
        embedServer.start(address, port, "xxl-job-executor-sample", accessToken);
        return ReturnT.SUCCESS;
    }


    @RequestMapping("/trigger")
    @ResponseBody
    public ReturnT<String> triggerJob(int id, String executorParam, String addressList) {
        if (Objects.isNull(executorParam)) {
            executorParam = "";
        }
        //触发器类型，手动 ，重试次数，'执行器任务分片参数，格式如 1/2'，任务参数，机器地址
        JobTriggerPoolHelper.trigger(id, TriggerTypeEnum.MANUAL, -1, null, executorParam, addressList);
        return ReturnT.SUCCESS;
    }

}
